<?php
namespace SimpleFactory;

include_once "Operation/OperationAdd.php";
include_once "Operation/OperationSubtract.php";
include_once "Operation/OperationMul.php";
include_once "Operation/OperationDiv.php";

use SimpleFactory\Operation\ {OperationAdd, OperationSubtract, OperationMul, OperationDiv};
class OperationFactory
{
    /** @var string 实体类标识 */
    const OPERATION_ADD = "+";
    const OPERATION_SUBTRACT = "-";
    const OPERATION_MUL = "*";
    const OPERATION_DIV = "/";
    /** @var array|string[] 存放实体类 */
    private array $operateClass = [
        self::OPERATION_ADD => OperationAdd::class,
        self::OPERATION_SUBTRACT => OperationSubtract::class,
        self::OPERATION_MUL => OperationMul::class,
        self::OPERATION_DIV => OperationDiv::class
    ];

    /**
     * 根据表示创建对应实体类对象
     * @param string $operate
     * @return mixed|string
     */
    public function createOperation(string $operate)
    {
        try {
            return new $this->operateClass[$operate];
        } catch (\Exception $e) {
            return $e->getMessage();
        }
    }
}